﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cstdx.Exchange;
namespace cstdx.Internal;


/// <summary>
/// 交易单位处理委托
/// </summary>
/// <param name="marketId">市场类型</param>
/// <param name="code">代码</param>
/// <returns>交易单位</returns>
public delegate double UnitHandler(MarketType marketId, string code);

/// <summary>
/// 交易单位处理类
/// </summary>
public static class BaseUnit
{
	private static UnitHandler _handler = DefaultBaseUnit;

	/// <summary>
	/// 默认交易单位处理方法
	/// </summary>
	/// <remarks>
	/// A股、债券交易和债券买断式回购交易的申报价格最小变动单位为0.01元人民币
	/// 基金、权证交易为0.001元人民币
	/// B股交易为0.001美元
	/// 债券质押式回购交易为0.005元
	/// </remarks>
	public static double DefaultBaseUnit(MarketType marketId, string code)
	{
		double unit = 100.00;

		if (marketId == MarketType.MarketIdShangHai)
		{
			if (code.Length >= 2)
			{
				string c = code.Substring(0, 2);
				if (c == "51")
				{
					unit = 1000.00;
				}
			}
		}
		else if (marketId == MarketType.MarketIdShenZhen)
		{
			if (code.Length >= 3)
			{
				string c = code.Substring(0, 3);
				if (c == "159")
				{
					unit = 1000.0;
				}
			}
		}

		return unit;
	}

	/// <summary>
	/// 获取交易单位
	/// </summary>
	public static UnitHandler Handler => _handler;

	/// <summary>
	/// 注册自定义交易单位处理方法
	/// </summary>
	/// <param name="handler">自定义处理方法</param>
	public static void RegisterBaseUnitFunction(UnitHandler handler)
	{
		if (handler == null)
			throw new ArgumentNullException(nameof(handler));

		_handler = handler;
	}
}
